<div class="container">
  <h1>_attempt_get_valid_token()</h1>
  <p class="signature">public function _attempt_get_valid_token($user_levels = null): string|bool</p>
  <h2>Description</h2>
  <div class="description">
    <p>
      Attempts to validate and return a token based on optional user level(s) condition.
      This method checks for a valid token in the following locations, in order of priority:
    </p>
    <ul>
      <li>HTTP headers (<code>$_SERVER['HTTP_TRONGATETOKEN']</code>)</li>
      <li>Cookies (<code>$_COOKIE['trongatetoken']</code>)</li>
      <li>Session (<code>$_SESSION['trongatetoken']</code>)</li>
    </ul>
    <p>
      The method then verifies the token against the provided user levels, if any, and returns the valid token if found.
    </p>
  </div>
  <h2>Parameters</h2>
  <table>
    <thead>
      <tr>
        <th>Parameter</th>
        <th>Type</th>
        <th>Description</th>
        <th>Default</th>
        <th>Required</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><code>$user_levels</code></td>
        <td>int|array|null</td>
        <td>User levels to filter tokens.</td>
        <td><code>null</code></td>
        <td>No</td>
      </tr>
    </tbody>
  </table>
  <h2>Return Value</h2>
  <table>
    <thead>
      <tr>
        <th>Type</th>
        <th>Description</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>string|bool</td>
        <td>The valid token if found, or false if none is found.</td>
      </tr>
    </tbody>
  </table>

  <h2>Attaching Tokens To Headers Using XMLHttpRequest</h2>
  <p>The code below demonstrates how to attach a 'Trongate token' to an <b>HTTP GET request</b> using JavaScript.  In the example, a PHP variable named <code>$trongate_token</code> has been made available within the JavaScript, and is being assigned to a JavaScript constant named, <b>token</b>.</p>
  <pre><code>&lt;script&gt;
const targetUrl = 'your_api_endpoint_here';
const token = '&lt;?= $trongate_token ?&gt;';

const http = new XMLHttpRequest();
http.open('GET', targetUrl);
http.setRequestHeader('Content-type', 'application/json');
http.setRequestHeader('trongateToken', token);
http.send();
http.onload = function() {
  console.log(http.status);
  console.log(http.responseText);
}
&lt;/script&gt;</code></pre>
<p>The code below demonstrates similar usage of XMLHttpRequest.  However, in this example we are submitting values to an endpoint via an <b>HTTP POST request</b>, using JavaScript.</p>
<pre><code>&lt;script&gt;
const targetUrl = 'your_api_endpoint_here';
const token = '&lt;?= $trongate_token ?&gt;';

const params = {
  firstName: 'John',
  lastName: 'Smith',
  age: 21
}

const http = new XMLHttpRequest();
http.open('POST', targetUrl);
http.setRequestHeader('Content-type', 'application/json');
http.setRequestHeader('trongateToken', token);
http.send(params);
http.onload = function() {
  console.log(http.status);
  console.log(http.responseText);
}
&lt;/script&gt;</code></pre>

<h2>Attaching Tokens To Headers Using Fetch API</h2>
<p>The Fetch API provides an alternative method for making HTTP requests in JavaScript, offering a more modern and flexible approach. Below are examples of how to achieve the same functionality as demonstrated with XMLHttpRequest, but using the Fetch API instead.</p>
<p>The code below demonstrates how to attach a 'Trongate token' to an <b>HTTP GET request</b> using the Fetch API.</p>
<pre><code>&lt;script&gt;
const targetUrl = 'your_api_endpoint_here';
const token = '&lt;?= $trongate_token ?&gt;';
fetch(targetUrl, {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json',
        'trongateToken': token
    }
})
.then(response => response.json())
.then(data => {
    console.log(data);
})
.catch(error => console.error('Error:', error));
&lt;/script&gt;</code></pre>

<p>The code below demonstrates similar usage of the Fetch API. However, in this example, we are submitting values to an endpoint via an <b>HTTP POST request</b>.</p>
<pre><code>&lt;script&gt;
const targetUrl = 'your_api_endpoint_here';
const token = '&lt;?= $trongate_token ?&gt;';

const params = {
    firstName: 'John',
    lastName: 'Smith',
    age: 21
}

fetch(targetUrl, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'trongateToken': token
    },
    body: JSON.stringify(params)
})
.then(response => response.json())
.then(data => {
    console.log(data);
})
.catch(error => console.error('Error:', error));
&lt;/script&gt;</code></pre>

<h2>Example Usage #1</h2>
  <p>The PHP code sample below demonstrates how to fetch <b>any</b> valid 'Trongate token' from <b>any</b> user level.</p>
  <div class="example">
    <pre><code>$this->module('trongate_tokens');
$token = $this->trongate_tokens->_attempt_get_valid_token(); // Any user level</code></pre>
  </div>
  <h2>Example Usage #2</h2>
  <p>The code below demonstrates how to fetch and return a valid 'Trongate token' for a logged-in user with an '<b>admin</b>' user level. This example assumes that the 'trongate_user_levels' database table contains a row with an 'id' of 1 and a 'level_title' of 'admin'.</p>
  <div class="example">
    <pre><code>$this->module('trongate_tokens');
$token = $this->trongate_tokens->_attempt_get_valid_token(1); // admin</code></pre>
  </div>
  <h2>Example Usage #3</h2>
  <p>In the final example, the code attempts to fetch and return a valid 'Trongate token' for a user with <b>either an 'admin' or a 'member'</b> user level. This example assumes that the 'trongate_user_levels' database table contains two specific rows: one with an 'id' of 1 and a 'level_title' of 'admin', and another with an 'id' of 2 and a 'level_title' of 'member'.</p>
  <div class="example">
    <pre><code>$this->module('trongate_tokens');
$token = $this->trongate_tokens->_attempt_get_valid_token([1, 2]); // admin or member</code></pre>
  </div>
</div>
